﻿/**********************************************************************/
/*                                                                    */
/*                   embDocumentInhalator                             */
/*             http://embdocumentinhalator.codeplex.com/              */
/*                                                                    */
/*               (c) Copyright 2010 Ilija Injac         .             */
/*                                                                    */
/*  This source is subject to the CDDL License                        */
/*  http://embdocumentinhalator.codeplex.com/license                  */
/*                                                                    */
/*                                                                    */
/**********************************************************************/
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace embDocumentInhalator
{
    /// Helps with file-system issues like generating valid sharepoint names
    /// from invalid ones.
    /// </summary>
    /// This element was inserted by injac on machine PLATINCHECKER.
    /// 27.01.2011,01:01
    public class FileSystemHelpers
    {

        /// <summary>
        /// Converts the name of a given filename as string,to valid share point filename.
        /// </summary>
        /// <param name="originalFileName">Name of the original file.</param>
        /// <returns></returns>
        /// This element was inserted by injac on machine PLATINCHECKER.
        /// 27.01.2011,01:02
        public string ConvertToValidSharePointFileName(string originalFileName)
        {
            char[] InvalidFilenameCharacters = new[] {'\\', '/', ':', '*', 
                    '?', '"', '<', '>', '|', '#', '{', '}', '%', '~','&' };

            string[] InvalidFileNameStrings = new[] { ".files", "_files",
                "-Dateien", "_fichiers", "_bestanden","_file",
                "_archivos", "-filer", "_tiedostot", "_pliki",
                "_soubory","_elemei", "_ficheiros", "_arquivos",
                "_dosyalar", "_datoteke","_fitxers", "_failid",
                "_fails", "_bylos", "_fajlovi", "_fitxategiak"};

            string validSharePointFileName = originalFileName;

            //Remove Invalid characters
            foreach (char c in InvalidFilenameCharacters)
            {
                while (validSharePointFileName.Contains(c.ToString()))
                {
                    if (validSharePointFileName.IndexOf(c) >= 0)
                    {
                        validSharePointFileName =
                        validSharePointFileName.Remove(validSharePointFileName.IndexOf(c), 1);
                    }
                }
            }

            //Remove consecutive periods(..)
            while (validSharePointFileName.Contains(".."))
            {
                validSharePointFileName = validSharePointFileName.Replace("..", ".");
            }

            //Remove reserved words from end
            foreach (string s in InvalidFileNameStrings)
            {
                if (validSharePointFileName.EndsWith(s))
                {
                    if (validSharePointFileName.Length > s.Length)
                    {
                        validSharePointFileName = validSharePointFileName.Substring(0, validSharePointFileName.Length - s.Length);
                    }
                }
            }

            //Remove period (.) at end
            if (validSharePointFileName.EndsWith("."))
            {
                validSharePointFileName = validSharePointFileName.Remove(validSharePointFileName.Length - 1, 1);
            }

            //Remove period (.) at end
            if (validSharePointFileName.StartsWith("."))
            {
                validSharePointFileName = validSharePointFileName.Remove(0, 1);
            }


            if (validSharePointFileName == string.Empty)
            {
                return string.Format("{0}{1}{2}{3}{4}{5}{6}", System.DateTime.Now.Day, System.DateTime.Now.Month,
                                                     System.DateTime.Now.Year, System.DateTime.Now.Hour,
                                                     System.DateTime.Now.Minute, System.DateTime.Now.Second, "AUTOGEN");
            }

            return validSharePointFileName;
        }
    }
}
